package com.mingweicai.intergration.redis.cluster.configuration;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPoolConfig;


//@Configuration
//@ConditionalOnBean(RedisClusterProperties.class)
public class RedisPoolConfig {

  private Logger logger = LoggerFactory.getLogger(RedisPoolConfig.class);

  @Bean
  public JedisPoolConfig jedisPoolConfig(RedisClusterProperties redisClusterProperties){
    logger.debug("RedisClusterProperties:{}",redisClusterProperties);
    JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
    // 最大空闲连接数, 默认8个
    jedisPoolConfig.setMaxIdle(redisClusterProperties.getMaxIdle());
    // 最大连接数, 默认8个
    jedisPoolConfig.setMaxTotal(redisClusterProperties.getMaxTotal());
    //最小空闲连接数, 默认0
    jedisPoolConfig.setMinIdle(redisClusterProperties.getMinIdle());
    // 获取连接时的最大等待毫秒数(如果设置为阻塞时BlockWhenExhausted),如果超时就抛异常, 小于零:阻塞不确定的时间,  默认-1
    jedisPoolConfig.setMaxWaitMillis(redisClusterProperties.getMaxWait()); // 设置2秒
    //对拿到的connection进行validateObject校验
    jedisPoolConfig.setTestOnBorrow(true);
    return jedisPoolConfig;
  }


}
